use std::thread::sleep;

///实现一个简单的单链表
// 维护一个链表头部
type NodePtr<T> = Option<Box<Node<T>>>;
#[derive(Debug)]
pub struct LinkList<T> {
    head: NodePtr<T>,
}
// 链表节点本身
#[derive(Debug)]
struct Node<T> {
    ele: T,
    next: NodePtr<T>,
}

impl<T> LinkList<T> {
    pub fn new() -> LinkList<T> {
        LinkList { head: None }
    }

    pub fn peek(&self) -> Option<&T> {
        self.head.as_ref().map(|node| &node.ele)
    }

    pub fn pop(&mut self) -> Option<T> {
        self.head.take().map(|node| {
            self.head = node.next;
            node.ele
        })
    }

    pub fn push(&mut self, ele: T) {
        self.head = Some(Box::new(Node {
            ele,
            next: self.head.take(),
        }));
    }

    pub fn into_iter(self) -> IntoIter<T> {
        IntoIter(self)
    }

    pub fn iter(&self) -> Iter<'_, T> {
        Iter(self.head.as_ref().map(|node| node.as_ref()))
    }

    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
        IterMut(self.head.as_mut().map(|node| node.as_mut()))
    }

    pub fn to_vec(self) -> Vec<T> {
        let mut vec = Vec::<T>::new();
        for x in self.into_iter() {
            vec.push(x);
        }
        vec.reverse();
        vec
    }

    pub fn reverse(&mut self) {
        if let Some(mut head) = self.head.take() {
            let mut old = head.next.take();
            while let Some(mut node) = old {
                old = node.next.replace(head);
                head = node;
            }
            self.head = Some(head);
        }
    }

    pub fn reverse_mut(&mut self, mut f_mut: impl FnMut(&mut T) -> ()) {
        if let Some(mut head) = self.head.take() {
            f_mut(&mut head.ele);
            let mut old_next = head.next.take();
            while let Some(mut node) = old_next {
                old_next = node.next.replace(head);
                head = node;
                f_mut(&mut head.ele);
            }
            self.head = Some(head);
        }
    }
}
// From
impl<T> From<Vec<T>> for LinkList<T> {
    fn from(mut value: Vec<T>) -> Self {
        let mut list = LinkList::new();
        value.reverse();
        value.into_iter().for_each(|v| {
            list.push(v);
        });
        list
    }
}

// Drop
impl<T> Drop for LinkList<T> {
    fn drop(&mut self) {
        let mut old_head = self.head.take();
        while let Some(mut node) = old_head {
            old_head = node.next.take();
        }
    }
}
// 三种迭代器
// iter
pub struct Iter<'a, T>(Option<&'a Node<T>>);
//
impl<'a, T> Iterator for Iter<'a, T> {
    type Item = &'a T;

    fn next<'b>(&'b mut self) -> Option<&'a T> {
        self.0.map(|node| {
            self.0 = node.next.as_deref();
            &node.ele
        })
    }
}
// iter_mut
pub struct IterMut<'a, T>(Option<&'a mut Node<T>>);

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.take().map(|node| {
            self.0 = node.next.as_deref_mut();
            &mut node.ele
        })
    }
}
// into_iter
pub struct IntoIter<T>(LinkList<T>);

impl<T> Iterator for IntoIter<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.pop()
    }
}

#[test]
pub fn test1() {
    let mut list = LinkList::new();
    list.push(1);
    list.push(2);
    // println!("{list:?}");
    // let ele = list.peek().unwrap();
    // println!("{list:?}");
    // println!("{}", ele);
    // let pop_ele = list.pop().unwrap();
    // println!("{pop_ele}");
    // println!("{list:?}");

    // let vec: _ = list.to_vec();
    // println!("{:?}", vec);
    // for x in list.iter() {
    //     println!("{x}");
    // }
    //
    // for x in list.iter_mut() {
    //     *x = 3;
    // }

    println!("{:?}", list);
    list.reverse_mut(|ele| *ele += 1);
    println!("{:?}", list);
}
